package com.yuejian_06.yuejian_06.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.yuejian_06.yuejian_06.Listener.MeListener;
import com.yuejian_06.yuejian_06.R;
import com.yuejian_06.yuejian_06.bean.SuccessBean;
import com.yuejian_06.yuejian_06.model.HomeModel;
import com.yuejian_06.yuejian_06.model.MeModel;

public class PostInfoActivity extends BaseActivity {
    private TextView user_name,createtime,content;
    private ImageView user_pic,pic1,pic2,pic3;
    private ImageButton back;
    private Button delpost;

    @Override
    void initViews() {

    }

    @Override
    void initEvents() {

    }

    @Override
    void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_info);
        initView();//初始化事件
        initEvent();
    }

    private void initView(){
        user_name = (TextView) findViewById(R.id.user_name);
        createtime = (TextView) findViewById(R.id.createtime);
        content = (TextView) findViewById(R.id.content);
        user_pic = (ImageView) findViewById(R.id.user_pic);
        pic1 = (ImageView) findViewById(R.id.pic1);
        pic2 = (ImageView) findViewById(R.id.pic2);
        pic3 = (ImageView) findViewById(R.id.pic3);
        back = (ImageButton) findViewById(R.id.back);
        delpost = (Button) findViewById( R.id.delpost);

        Intent intent = getIntent();
        user_name.setText(intent.getStringExtra("user_name"+""));
        createtime.setText(intent.getStringExtra("createtime"+""));
        content.setText(intent.getStringExtra("content"+""));
        if (!"".equals(intent.getStringExtra("user_pic")))
            Picasso.with(PostInfoActivity.this).load("http://10.0.2.2/yuejian/Public/postpics/"+intent.getStringExtra("user_pic")).into(user_pic);
        if (!"".equals(intent.getStringExtra("pic1")))
            Picasso.with(PostInfoActivity.this).load("http://10.0.2.2/yuejian/Public/postpics/"+intent.getStringExtra("pic1")).into(pic1);
        if (!"".equals(intent.getStringExtra("pic2")))
            Picasso.with(PostInfoActivity.this).load("http://10.0.2.2/yuejian/Public/postpics/"+intent.getStringExtra("pic2")).into(pic2);
        if (!"".equals(intent.getStringExtra("pic3")))
            Picasso.with(PostInfoActivity.this).load("http://10.0.2.2/yuejian/Public/postpics/"+intent.getStringExtra("pic3")).into(pic3);

    }

    private void initEvent(){
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        delpost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(PostInfoActivity.this);
                builder.setIcon(R.drawable.heart_p);
                builder.setTitle(R.string.del);
                builder.setMessage(R.string.del_post);
                builder.setPositiveButton(R.string.postive_button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = getIntent();
                        String post_id1 = intent.getStringExtra("post_id");
                        MeModel meModel = new MeModel();
                        meModel.getDelPost(post_id1,meListener);
                    }
                });

                builder.setNegativeButton(R.string.negative_button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(PostInfoActivity.this,"已经取消",Toast.LENGTH_SHORT).show();
                    }
                });

                builder.setCancelable(false);
                AlertDialog dialog=builder.create();
                dialog.show();

            }
        });
    }

    private MeListener<SuccessBean> meListener = new MeListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean o) {
            Toast.makeText(PostInfoActivity.this,"删除帖子成功",Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(PostInfoActivity.this,PostListActivity.class);
            startActivityForResult(intent,1);
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(PostInfoActivity.this,"删除帖子失败",Toast.LENGTH_SHORT).show();
        }
    };
}
